<template>
  <el-container class="home-container">
    <el-header>
      首页
      <img src="../assets/logo.png" alt="" class="logo">
      <button @click="cancel">注销</button>
    </el-header>
    <el-container>
      <!-- 侧边 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleMenu">
          |||
        </div>
        <el-menu background-color="#333744" :unique-opened="true" text-color="#fff" active-text-color="red"
          :default-active='currentPath' :collapse="isCollapse" :collapse-transition="false">
          <!-- 一级菜单 -->
          <el-submenu :index="usersub.path" :key="usersub.id" v-for="usersub in UserList">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="icon[usersub.id]"></i>
              <!-- 文本 -->
              <span>{{usersub.authName}}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item @click='toPath(item.path)' :index="item.path" :key="item.id" v-for="item in usersub.children">
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-location"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>
        <!--  -->
      </el-aside>
      <!-- main -->
      <el-main>
        <router-view>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    components: {},
    data() {
      return {
        isCollapse: false,
        UserList: [],
        icon: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-3702mima',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        },
        currentPath: null
      };
    },
    created() {

      this.getUserList()
      this.currentPath = sessionStorage.getItem('cPath')
    },
    mounted() {},
    watch: {},
    computed: {},
    methods: {
      cancel: function () {
        sessionStorage.clear();
        this.$router.push("/login");
      },
      getUserList() {
        this.$axios.get('menus').then(val => {
          this.UserList = val.data

        })
      },
      // 跳转路由
      toPath(val) {
        this.$router.push('/' + val)
        sessionStorage.setItem('cPath', val)
      },
      toggleMenu() {
        this.isCollapse = !this.isCollapse
      },

    },
  };

</script>
<style lang="css" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
  }

  .el-aside {
    height: 100%;
    background-color: #333744;
  }

  .el-main {
    background-color: #eaedf1;
  }

  .logo {
    vertical-align: middle;
    width: 50px;
  }

  .el-submenu {
    text-align: left;
  }

  span {
    margin: 0 15px;
  }

  .toggle-button {
    background-color: #fff;
    color: #eaedf1;
    cursor: pointer;
  }

  .el-menu {
    border: none;
  }

</style>
